<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Users;

class UsersController extends Controller
{
    public function actionRegister(){
    	return $this->render('register');
    }
    public function actionRegister_do(){
    	$post=Yii::$app->request->post();
    	if (!empty($post)) {
    		$data=[
              'username'=>$post['username'],
              'pwd'=>md5($post['pwd']),
              'qpwd'=>md5($post['qpwd']),
              'email'=>$post['email'],
              'photo'=>$post['photo']
    		];
    		if ($data['pwd'] != $data['qpwd']) {
    			echo "<script>alert('两次密码不一致');location.href='?r=users/register'</script>";
    		}
    		elseif (empty($data['email'])){
    			echo "<script>alert('邮箱和手机号至少填一项');location.href='?r=users/register'</script>";
    		}
    		elseif (empty($data['photo'])){
    			echo "<script>alert('邮箱和手机号至少填一项');location.href='?r=users/register'</script>";
    		}	
    	 	elseif($data['pwd'] == $data['qpwd'] && !empty($data['email'])==true){
		    	$customer = new Users();
		        $customer->username =$data['username'];
		        $customer->pwd =$data['pwd'];
		        $customer->email =$data['email'];
		        $customer->photo =$data['photo'];
		        if ($customer->save()) {
		        	echo "<script>alert('注册成功')</script>";
		        }
    		}
    	
    	}
    	
    }
    public function actionLogin()
    {
    	return $this->render('login');
    }
    public function actionLogin_do()
    {
    	$username=Yii::$app->request->post('username');
    	$pwd=Yii::$app->request->post('pwd');
    	$sql="SELECT * FROM users WHERE username=$username AND pwd=$pwd";
    	$res = Yii::$app->db->createCommand($sql)
           ->queryOne();
       
           echo "<script>alert('登录成功');location.href='?r=users/qp'</script>";
        
    }
    public function actionQp()
    {
    	for ($i=0; $i <=10 ; $i++) { 
    		for ($j=0; $j <=$i ; $j++) { 
   			   if ($i%2==0) {
   			   	echo "□";
   			   }
   			   elseif($j%2==0){
                 echo "■"."<br/>";
   			   }
    		}
    	}
    }
}